package cn.iocoder.yudao.module.erp.service.outinrule;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.erp.controller.admin.outinrule.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.outinrule.OutInRuleDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.erp.dal.mysql.outinrule.OutInRuleMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;

/**
 * ERP 出入库规则 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OutInRuleServiceImpl implements OutInRuleService {

    @Resource
    private OutInRuleMapper outInRuleMapper;

    @Override
    public Long createOutInRule(OutInRuleSaveReqVO createReqVO) {
        // 插入
        OutInRuleDO outInRule = BeanUtils.toBean(createReqVO, OutInRuleDO.class);
        outInRuleMapper.insert(outInRule);
        // 返回
        return outInRule.getId();
    }

    @Override
    public void updateOutInRule(OutInRuleSaveReqVO updateReqVO) {
        // 校验存在
        validateOutInRuleExists(updateReqVO.getId());
        // 更新
        OutInRuleDO updateObj = BeanUtils.toBean(updateReqVO, OutInRuleDO.class);
        outInRuleMapper.updateById(updateObj);
    }

    @Override
    public void deleteOutInRule(Long id) {
        // 校验存在
        validateOutInRuleExists(id);
        // 删除
        outInRuleMapper.deleteById(id);
    }

    private void validateOutInRuleExists(Long id) {
        if (outInRuleMapper.selectById(id) == null) {
            throw exception(OUT_IN_RULE_NOT_EXISTS);
        }
    }

    @Override
    public OutInRuleDO getOutInRule(Long id) {
        return outInRuleMapper.selectById(id);
    }

    @Override
    public PageResult<OutInRuleDO> getOutInRulePage(OutInRulePageReqVO pageReqVO) {
        return outInRuleMapper.selectPage(pageReqVO);
    }

}